package com.rie.test;
import com.rie.dao.DAO;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import rie.api.SystemeInfoFactory;
import rie.model.Processus;
import rie.model.SystemeInfo;
import rie.model.CPU;
import rie.model.Memoire;
import rie.model.Processus;
import rie.model.SystemeInfo;

/**
 *
 * @author Habib
 */
@WebServlet(name = "displaySystemInfo", urlPatterns = {"/displaySystemInfo"})
public class DisplaySystemInfo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            out.println("<html>");
            out.println("<head>");
             out.println(" <link href='bootstrap/css/bootstrap-responsive.min.css' rel='stylesheet' type='text/css'>\n" +
"        <link href='bootstrap/css/bootstrap.css' rel='stylesheet' type='text/css'>\n" +
"        <link href='bootstrap/css/bootstrap-responsive.css' rel='stylesheet' type='text/css'>");
            out.println("<title>Servlet Formulaire</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Formulaire de création d'un produit</h1>");
            out.println("<form method=\"post\">");
            out.println("<label for=\"id\">ID : </label>");
            out.println("<input type=\"text\" id=\"id\" name=\"id\"/>");
            out.println("<label for=\"description\">Description : </label>");
            out.println("<input type=\"text\" id=\"descr\" name=\"descr\"/>");
            out.println("<input type=\"submit\" />");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String[] methodes = request.getParameterValues("methode");
        String serveur = request.getParameter("serveur");
        String comment = request.getParameter("commentaires");

        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Formulaire</title>");
            out.println("</head>");
            out.println("<body>");
            if (serveur != null && !serveur.isEmpty()) { 
                out.println("Vous avez interrogé le serveur '" + serveur + "'.<br/><br/>");
                try {
                    Registry myRegistry = LocateRegistry.getRegistry(serveur, 1899);        
                    SystemeInfo obj = null;   
                    for (String s: myRegistry.list()) {
                        System.out.println(s);
                    }
                    obj = (SystemeInfo)((SystemeInfoFactory) myRegistry.lookup("monito")).createSysteme();
                    System.out.println(obj);

                    if (obj.getErreur() == null || obj.getErreur().isEmpty()) {

                        if (comment != null && !comment.isEmpty()) {
                            obj.setCommentaire(comment);
                            out.println("Commentaire : " + comment + "<br/>");
                        } else {
                            obj.setCommentaire("Aucun commentaire.");
                        }

                        if (methodes != null && methodes.length > 0) {
                            boolean saveCpu = false;
                            boolean saveMemoire = false;
                            boolean saveProcess = false;
                            for (int i = 0; i < methodes.length; i++) {
                                if (methodes[i].equals("cpu")) {
                                    out.println("Sa charge CPU est de " + obj.getCpu().getLoadCPU() + "%.<br/>");
                                    saveCpu = true;
                                }
                                if (methodes[i].equals("memoire")) {
                                    out.println("Sa mémoire RAM libre est de " + obj.getMemoire().getFreeRam() + "Mo.<br/>");
                                    saveMemoire =true;
                                }
                                if (methodes[i].equals("processus")) {
                                    out.println("Les processus courants");
                                    out.println("<table>");
                                    out.println("<tr><th>Nom</th><th>PID</th><th>Mémoire</th></tr>");
                                    for (Processus p : obj.getListProcessus()) {
                                        out.println("<tr><td>" + p.getName() + "</td><td>" + p.getPid() + "</td><td>" + p.getMemoryUsage() + "</td></tr>");
                                    }
                                    out.println("</table>");
                                    saveProcess = false;
                                }
                            }
                            DAO reportDao = new DAO();
                            reportDao.saveQuery(obj,saveCpu,saveMemoire,saveProcess);
                        }
                    }else{
                       out.println(obj.getErreur()+"<br/>"); 
                       out.println("Veuillez réessayer ultérieurement."); 
                    }                    
                    out.println("<a href='./index.jsp'>Retour à la page de recherche</a>");
                } catch (Exception ioe) {
                    System.out.println("Connexion au serveur RMI échouée.");
                } 
            } else {
                out.println("Renseigner des informations valides <br/>");
                out.println("<a href='./index.jsp' >Oui</a>");
            }
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
